import common from './common.js'
// import { api } from './api.js'

class Login {
	h5Login = () => {
		common.getWechatCode() 
	}
	
	mpWeiXinLogin = () => {
		common.navigateTo('/pages/public/login')
	}
	
	appLogin = () => {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				// 获取用户信息
				uni.getUserInfo({
					provider: 'weixin',
					success: function(res) {
						res = res.userInfo
						let sourceUserId = common.getStorage('sourceUserId')
						let temp = { openId: res.openId, nickName: res.nickName, avatarUrl: res.avatarUrl, unionId: res.unionId, gender: res.gender, sourceUserId }
						/* api.loginFromAppAsync(temp, res => {
							let now = new Date()
							let nowTime = now.getTime()
							let expireTime = now.setTime(nowTime + 5 * 24 * 60 * 60 * 1000)
							res.expireTime = now.getTime()
							common.setUserInfo(res)
						}) */
					},
					fail(errGetUserInfo) {
						console.error(errGetUserInfo);
					}
				});
			},
			fail(errLogin) {
				console.error(errLogin);
			}
		});
	}
		
	uniLogin = () => {
		// #ifdef H5 
		this.h5Login()
		// #endif
		
		// #ifdef APP-PLUS
		this.appLogin()
		// #endif
		
		// #ifdef MP-WEIXIN
		this.mpWeiXinLogin()
		// #endif
	}
}

export default new Login